package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IDraftContractService;
import com.mdd.admin.validate.DraftContractCreateValidate;
import com.mdd.admin.validate.DraftContractUpdateValidate;
import com.mdd.admin.validate.DraftContractSearchValidate;
import com.mdd.admin.vo.DraftContractListedVo;
import com.mdd.admin.vo.DraftContractDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.DraftContract;
import com.mdd.common.mapper.DraftContractMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 草稿实现类
 * @author LikeAdmin
 */
@Service
public class DraftContractServiceImpl implements IDraftContractService {
        
    @Resource
    DraftContractMapper draftContractMapper;

    /**
     * 草稿列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<DraftContractListedVo>
     */
    @Override
    public PageResult<DraftContractListedVo> list(PageValidate pageValidate, DraftContractSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<DraftContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        draftContractMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "datetime:createTimeStart-createTimeEnd@create_time:str",
            "=:contractId@contract_id:str",
            "like:contractName@contract_name:str",
            "like:customerName@customer_name:str",
            "=:partybFirm@partyB_firm:str",
            "=:contractType@contract_type:str",
            "=:amount:long",
            "=:creator:str",
            "=:finalProcessingTime@final_processing_time:str",
            "=:fieldStaff@field_staff:str",
            "=:fieldStaffId@field_staff_id:str",
            "=:correspondingBusiness@corresponding_business:str",
            "=:contractState@contract_state:str",
        });

        IPage<DraftContract> iPage = draftContractMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<DraftContractListedVo> list = new LinkedList<>();
        for(DraftContract item : iPage.getRecords()) {
            DraftContractListedVo vo = new DraftContractListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 草稿详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return DraftContract
     */
    @Override
    public DraftContractDetailVo detail(Integer id) {
        DraftContract model = draftContractMapper.selectOne(
                new QueryWrapper<DraftContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        DraftContractDetailVo vo = new DraftContractDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 草稿新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(DraftContractCreateValidate createValidate) {
        DraftContract model = new DraftContract();
        model.setCreateTime(System.currentTimeMillis() / 1000);
        draftContractMapper.insert(model);
    }

    /**
     * 草稿编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(DraftContractUpdateValidate updateValidate) {
        DraftContract model = draftContractMapper.selectOne(
                new QueryWrapper<DraftContract>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        draftContractMapper.updateById(model);
    }

    /**
     * 草稿删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        DraftContract model = draftContractMapper.selectOne(
                new QueryWrapper<DraftContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        draftContractMapper.updateById(model);
    }

}
